﻿app.controller('VdDictAddCtrl', function ($scope, $modalInstance, $interval, type, selected, deDictionary) {
    var originalData;
    var lastData;
    $scope.message = '';
    $scope.status = { open: true };
    var isEdit = (type == 'edit');
    if (isEdit) {
        $scope.type = '编辑';
        originalData = (selected && selected.length > 0) ? DeepClone(selected[0]) : null;
        $scope.deDictionary = selected[0];
    } else {
        $scope.type = '添加';
        $scope.deDictionary = {};
        $scope.deDictionary.StatusCode = 1;
    }

    $scope.getInputCode = function () {
        $scope.deDictionary.InputCode = GetFirstPinYinCode($scope.deDictionary.Value);
    };

    $scope.ok = function () {
        $modalInstance.close($scope.deDictionary);
    };

    $scope.cancel = function () {
        if (originalData) {
            selected[0] = originalData;
        }
        if (lastData) {
            $modalInstance.dismiss(lastData);
            return;
        }
        $modalInstance.dismiss();
    };

    $scope.okNext = function () {
        deDictionary.Add($scope.deDictionary).success(function (data, header, config, status) {
            $scope.message = '添加成功！（3）';
            $scope.seconds = 2;
            var message = $interval(function () {
                if ($scope.seconds <= 0) {
                    $interval.cancel(message);
                    $scope.message = '';
                } else {
                    $scope.message = '添加成功！（' + $scope.seconds + '）';
                    $scope.seconds--;
                }
            }, 1000);
            //第一个输入框获取焦点
            $scope.isFocus = true;
            $scope.deDictionary.SerialNo = data;
            lastData = $scope.deDictionary;
            $scope.deDictionary = {};
            $scope.deDictionary.StatusCode = 1;
        }).error(function (data, header, config, status) {
            angular.alert('操作失败！');
        });
    };
});
